package com.oigx.lookmu.ui;

import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.ui.TextBrowseFolderListener;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.ui.TitledSeparator;
import com.oigx.lookmu.core.LookMuUtil;
import com.oigx.lookmu.core.SettingsState;

import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.text.html.HTMLEditorKit;

public class SettingFrame {

    private JPanel mainPanel;
    private TitledSeparator settingSep;

    private JPanel baseSettings;
    private JLabel fileDirLabel;
    private TextFieldWithBrowseButton fieldWithBrowseButton;
    private JLabel lineMaxCharLabel;
    private JTextField lineMaxCharField;

    private TitledSeparator descSep;
    private JPanel descPanel;
    private JTextPane descTextPane;
    private JTextPane urlTextPane;

    public JComponent getRootComponent() {
        return mainPanel;
    }

    public String getFilePath() {
        return fieldWithBrowseButton.getText();
    }

    public String getLineMaxChar() {
        return lineMaxCharField.getText();
    }

    public SettingFrame(){
        descTextPane.setEditable(false);
        descTextPane.setText("1、打开配置界面File->settings->lookMuSettings \n" +
                "2、选中txt目录,apply后插件自动读取目录中的txt文件 \n" +
                "3、控制栏LookMu->books中双击选中txt \n" +
                "4、控制栏LookMu->chapter再选中章节 \n" +
                "5、单击状态栏中‘-->’ 显示下一行，或者快捷键 ctrl+-> 下一行，快捷键 ctrl+<- 上一行 \n" +
                "6、欢迎联系308181562@qq.com");
        
        urlTextPane.setEditable(false);
        HTMLEditorKit editorKit = new HTMLEditorKit();
        urlTextPane.setEditorKit(editorKit);
        urlTextPane.setContentType("text/html; charset=UTF-8");
        urlTextPane.setText("<a href='https://gitee.com/wuxiaolin-001/lookmu'><span style='font-family:\"Microsoft YaHei\";font-size:10px;'>更多说明欢迎关注</span></a>");
        urlTextPane.addHyperlinkListener(e -> {
            if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                System.out.println("Clicked on link: " + e.getDescription());
                LookMuUtil.openWebpage(e.getDescription());
            }
        });
        SettingsState settingsState = SettingsState.getInstance();
        if (settingsState.filePath != null){
            fieldWithBrowseButton.setText(settingsState.filePath);
        }
        if (settingsState.lineMaxChar!=null && settingsState.lineMaxChar>0){
            lineMaxCharField.setText(settingsState.lineMaxChar.toString());
        }
        FileChooserDescriptor chooserDescriptor = new FileChooserDescriptor(true,true,true,true,true,true);
        TextBrowseFolderListener listener = new TextBrowseFolderListener(chooserDescriptor);
        fieldWithBrowseButton.addBrowseFolderListener(listener);
    }

}

